/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hilton.quickcontrol;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.hilton.quickcontrol.PieStackView.OnCurrentListener;
import com.hilton.tigershark.BrowserActivity;
import com.hilton.tigershark.R;
import com.hilton.tigershark.Tab;

/**
 * Controller for Quick Controls pie menu
 */
public class PieControl implements PieMenu.PieController, OnClickListener {

    private static final String TAG = null;
    protected BrowserActivity mBrowserActivity;
    protected PieMenu mPie;
    protected int mItemSize;
    protected TextView mTabsCount;
    private PieItem mNewWindow;
    private PieItem mOptions;
    private PieItem mBookmarks;
    private PieItem mExit;
    private PieItem mWindows;
    private FrameLayout mContainer;

    public PieControl(BrowserActivity activity, FrameLayout container) {
        mBrowserActivity = activity;
        mItemSize = (int) activity.getResources().getDimension(R.dimen.qc_item_size);
        mContainer = container;
    }

    public void stopEditingUrl() {
    }

    public void attach() {
        if (mPie == null) {
            mPie = new PieMenu(mBrowserActivity);
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            mPie.setLayoutParams(lp);
            populateMenu();
            mPie.setController(this);
        }
        if (mPie.getParent() != null) {
            mContainer.removeView(mPie);
        }
        mContainer.addView(mPie);
    }

    public void detach() {
        if (mPie.getParent() == null) {
            Log.w(TAG, "Pie is not attached yet! do nothing");
            return;
        }
        mContainer.removeView(mPie);
    }

    public void forceToTop() {
        if (mPie.getParent() != null) {
            mContainer.removeView(mPie);
            mContainer.addView(mPie);
        }
    }

    protected void setClickListener(OnClickListener listener, PieItem... items) {
        for (PieItem item : items) {
            item.getView().setOnClickListener(listener);
        }
    }

    @Override
    public boolean onOpen() {
        /*int n = mUiController.getTabControl().getTabCount();
        mTabsCount.setText(Integer.toString(n));
        Tab tab = mUiController.getCurrentTab();
        if (tab != null) {
            mForward.setEnabled(tab.canGoForward());
        }
        WebView view = mUiController.getCurrentWebView();
        if (view != null) {
            ImageView icon = (ImageView) mRDS.getView();
            if (mUiController.getSettings().hasDesktopUseragent(view)) {
                icon.setImageResource(R.drawable.ic_mobile);
            } else {
                icon.setImageResource(R.drawable.ic_desktop_holo_dark);
            }
        }*/
        return true;
    }

    protected void populateMenu() {
        mBookmarks = makeItem(R.drawable.ic_btn_bookmarks, 1);
        mOptions = makeItem(R.drawable.ic_menu_list, 1);
        mExit = makeItem(android.R.drawable.ic_menu_close_clear_cancel, 1);
        mNewWindow = makeItem(R.drawable.ic_menu_new_window, 1);
        mWindows = makeItem(R.drawable.ic_menu_windows, 1);
        setClickListener(this, mExit, mBookmarks, mOptions, mNewWindow, mWindows);
        // level 1
        mPie.addItem(mExit);
        mPie.addItem(mOptions);
        mPie.addItem(mBookmarks);
        mPie.addItem(mNewWindow);
        mPie.addItem(mWindows);
    }

    @Override
    public void onClick(View v) {
        if (mBookmarks.getView() == v) {
            mBrowserActivity.bookmarksOrHistoryPicker();
        } else if (mExit.getView() == v) {
            mBrowserActivity.exitBrowser();
        } else if (mOptions.getView() == v) {
            mBrowserActivity.openPreferences();
        } else if (mNewWindow.getView() == v) {
            mBrowserActivity.openTabToHomePage();
        } else if (mWindows.getView() == v) {
            mBrowserActivity.showWindows();
        }
    }

    private void buildTabs() {
        /*final List<Tab> tabs = mUiController.getTabs();
        mUi.getActiveTab().capture();
        mTabAdapter.setTabs(tabs);
        PieStackView sym = (PieStackView) mShowTabs.getPieView();
        sym.setCurrent(mUiController.getTabControl().getCurrentPosition());*/
    }

    protected PieItem makeItem(int image, int l) {
        ImageView view = new ImageView(mBrowserActivity);
        view.setImageResource(image);
        view.setMinimumWidth(mItemSize);
        view.setMinimumHeight(mItemSize);
        view.setScaleType(ScaleType.CENTER);
        LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
        view.setLayoutParams(lp);
        return new PieItem(view, l);
    }

    protected PieItem makeFiller() {
        return new PieItem(null, 1);
    }

    protected View makeTabsView() {
        View v = mBrowserActivity.getLayoutInflater().inflate(R.layout.active_tabs, null);
        mTabsCount = (TextView) v.findViewById(R.id.label);
        mTabsCount.setText("1");
        ImageView image = (ImageView) v.findViewById(R.id.icon);
        image.setImageResource(R.drawable.ic_menu_windows);
        image.setScaleType(ScaleType.CENTER);
        LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
        v.setLayoutParams(lp);
        return v;
    }

    static class TabAdapter extends BaseAdapter implements OnCurrentListener {

        LayoutInflater mInflater;
        //UiController mUiController;
        private List<Tab> mTabs;
        private int mCurrent;

        public TabAdapter(Context ctx) {
            mInflater = LayoutInflater.from(ctx);
            //mUiController = ctl;
            mTabs = new ArrayList<Tab>();
            mCurrent = -1;
        }

        public void setTabs(List<Tab> tabs) {
            mTabs = tabs;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Tab getItem(int position) {
            return mTabs.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Tab tab = mTabs.get(position);
            View view = mInflater.inflate(R.layout.tabs, null);
            ImageView thumb = (ImageView) view.findViewById(R.id.thumb);
            //TextView title1 = (TextView) view.findViewById(R.id.title1);
            //TextView title2 = (TextView) view.findViewById(R.id.title2);
            Bitmap b = tab.getScreenshot();
            if (b != null) {
                thumb.setImageBitmap(b);
            }
            if (position > mCurrent) {
                //title1.setVisibility(View.GONE);
                //title2.setText(tab.getTitle());
            } else {
                //title2.setVisibility(View.GONE);
                //title1.setText(tab.getTitle());
            }
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //mUiController.switchToTab(tab);
                }
            });
            return view;
        }

        @Override
        public void onSetCurrent(int index) {
            mCurrent = index;
        }

    }

}
